import axios from '../utils/axios'

// 用户管理API
export const userApi = {
  // 获取用户列表
  getUserList() {
    return axios.get('/users/list')
  },

  // 根据ID获取用户信息
  getUserById(id) {
    return axios.get(`/users/${id}`)
  },

  // 创建用户
  createUser(userData, roleIds) {
    const params = new URLSearchParams()
    if (roleIds && roleIds.length > 0) {
      params.append('roleIds', roleIds.join(','))
    }
    return axios.post('/users', userData, { params })
  },

  // 更新用户
  updateUser(id, userData, roleIds) {
    const params = new URLSearchParams()
    if (roleIds && roleIds.length > 0) {
      params.append('roleIds', roleIds.join(','))
    }
    return axios.put(`/users/${id}`, userData, { params })
  },

  // 删除用户
  deleteUser(id) {
    return axios.delete(`/users/${id}`)
  },

  // 修改密码
  changePassword(id, oldPassword, newPassword) {
    const params = new URLSearchParams()
    params.append('oldPassword', oldPassword)
    params.append('newPassword', newPassword)
    return axios.post(`/users/${id}/change-password`, {}, { params })
  },

  // 重置密码
  resetPassword(id, newPassword) {
    const params = new URLSearchParams()
    params.append('newPassword', newPassword)
    return axios.post(`/users/${id}/reset-password`, {}, { params })
  },

  // 更新用户状态
  updateUserStatus(id, status) {
    const params = new URLSearchParams()
    params.append('status', status)
    return axios.put(`/users/${id}/status`, {}, { params })
  },

  // 分配角色给用户
  assignRoles(userId, roleIds) {
    return axios.post(`/users/${userId}/roles`, { roleIds })
  },

  // 获取用户的角色列表
  getUserRoles(userId) {
    return axios.get(`/users/${userId}/roles`)
  },

  // 移除用户的角色
  removeUserRole(userId, roleId) {
    return axios.delete(`/users/${userId}/roles/${roleId}`)
  },

  // 批量分配角色
  batchAssignRoles(userIds, roleIds) {
    return axios.post('/users/batch-assign-roles', { userIds, roleIds })
  }
}
